<?php
/**
 * Initialize the global $_genesis_settings array. Reduces calls to cache
 * and the number of times the setting has to be unserialized.
 * 
 * @since 1.2
 */
$_genesis_settings = apply_filters('genesis_global_settings_array', array(
	GENESIS_SETTINGS_FIELD => get_option(GENESIS_SETTINGS_FIELD),
	GENESIS_SEO_SETTINGS_FIELD => get_option(GENESIS_SEO_SETTINGS_FIELD)
));

/**
 * These functions pull options/settings
 * from the options database.
 *
 * @since 0.1.3
 **/
function genesis_get_option($key, $setting = null) {
	global $_genesis_settings;
	
	$setting = $setting ? $setting : GENESIS_SETTINGS_FIELD;
	
	// Allow child theme to short-circuit this function
	$pre = apply_filters('genesis_pre_get_option_'.$key, false, $setting);
	if( $pre ) return $pre;
	
	// Pull from $_genesis_settings global var, or directly from database
	$options = array_key_exists( $setting, (array)$_genesis_settings ) ? $_genesis_settings[$setting] : get_option($setting);
	
	// One last filter
	$options = apply_filters('genesis_options', $options, $setting);
	
	if ( ! array_key_exists( $key, (array)$options ) )
        return '';

	return wp_kses_stripslashes( wp_kses_decode_entities( $options[$key] ) );
	
}
function genesis_option($key, $setting = null) {
	echo genesis_get_option($key, $setting);
}
function genesis_get_seo_option($key) {
	return genesis_get_option($key, GENESIS_SEO_SETTINGS_FIELD);
}
function genesis_seo_option($key) {
	genesis_option($key, GENESIS_SEO_SETTINGS_FIELD);
}

/**
 * These functions can be used to easily and efficiently pull data from a
 * post/page custom field. Returns FALSE if field is blank or not set.
 *
 * @param string $field used to indicate the custom field key
 *
 * @since 0.1.3
 */
function genesis_custom_field($field) {
	echo genesis_get_custom_field($field);
}
function genesis_get_custom_field($field) {
	global $post;
	
	if (null === $post) return FALSE;
	
	$custom_field = get_post_meta($post->ID, $field, true);
	
	if($custom_field) {
		// sanitize and return the value of the custom field
		return wp_kses_stripslashes(wp_kses_decode_entities($custom_field));
	}
	else {
		// return FALSE if custom field is empty
		return FALSE;
	}
}